package com.test.mooan.labama.view_boom.module_boom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.test.mooan.labama.R;
import com.test.mooan.labama.bean_boom.product.Product;
import com.test.mooan.labama.bean_boom.user_info_boom.ActList_boom;
import com.test.mooan.labama.dialog_boom.ln_boom.AddBkActDialog_Fr_boom;
import com.test.mooan.labama.dialog_boom.ln_boom.DeleteBkActDialog_Fr_boom;
import com.test.mooan.labama.utils_boom.Threadutils_boom;
import com.test.mooan.labama.utils_boom.helper_boom.UserInfoHelper_boom;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class AddBkActView_Fr_boom extends LinearLayout {

    private LinearLayout groupLayout;
    private View addAccountBtnView;

    private String pageTag;

    private Product product;

    private final ArrayList<View> accountArrViews = new ArrayList<>();

    public AddBkActView_Fr_boom(Context context) {
        super(context);
        init(context);
    }

    public AddBkActView_Fr_boom(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public AddBkActView_Fr_boom(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){

        View view = View.inflate(context, R.layout.view_add_bk_act_fr_boom,this);
        groupLayout = view.findViewById(R.id.view_add_bank_account_layout_group);
        addAccountBtnView = view.findViewById(R.id.view_add_bank_account_layout_add_account_btn);

        addAccountBtnView.setOnClickListener(v -> {

            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_ADD_CLICK"));

            new AddBkActDialog_Fr_boom(context, pageTag,product).show();
        });

    }

    //更新账户列表
    public void updateAccountList(){
        accountArrViews.clear();
        groupLayout.removeAllViews();
        ArrayList<ActList_boom.BankAccount> accounts = ActList_boom.getInstance().gBankAccountList();

        for (ActList_boom.BankAccount account:accounts){
            addAccount(account);
        }

        if (accountArrViews.size()==1) accountArrViews.get(0).findViewById(R.id.item_wallet_account_ib_delete).setVisibility(GONE);

    }

    private void addAccount(ActList_boom.BankAccount bankAccount){

        View view = View.inflate(getContext(), R.layout.item_wlt_act_fr_boom, null);
        TextView accountTv = view.findViewById(R.id.item_wallet_account_tv_account);
        ImageView accountTypeIv = view.findViewById(R.id.item_wallet_account_iv_wallet_type);
        TextView accountTypeTv = view.findViewById(R.id.item_wallet_account_tv_wallet_type);
        ImageButton deleteIb = view.findViewById(R.id.item_wallet_account_ib_delete);

        accountArrViews.add(view);

        accountTv.setText(bankAccount.gWalletMobile());
        accountTypeTv.setText(bankAccount.gWalletDesc());
        deleteIb.setOnClickListener(v -> {

            UserInfoHelper_boom.gInstance().addEventPoint((pageTag+"_DELETE_CLICK"));

            new DeleteBkActDialog_Fr_boom(getContext(),bankAccount,pageTag,product).show();
        });

        groupLayout.addView(view);

        //获取图标
        if (bankAccount.gBitmap()!=null){
            accountTypeIv.setImageBitmap(bankAccount.gBitmap());
        }else {
            Threadutils_boom.getInstance().runOnChildThread(() -> {
                try {
                    if (TextUtils.isEmpty(bankAccount.gWalletLogo())) return;
                    URL url = new URL(bankAccount.gWalletLogo());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        bankAccount.sBitmap(bitmap);
                        Threadutils_boom.getInstance().runOnUiThread(() -> {
                            if (accountTypeIv!=null) accountTypeIv.setImageBitmap(bitmap);
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }

    }

    public void setPageTag(String pageTag) {
        this.pageTag = pageTag;
    }

    public void setProduct(Product product){
        this.product = product;
    }

}
